<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>事件处理</title>
    <style>
        body {
            font-family: "Lato", "Helvetica Neue", Helvetica, Arial, sans-serif;
            color: #34495e;
        }

        p {
            font-weight: 800;
        }

        input {
            padding: 0 10px;
            font-size: inherit;
            font-weight: 800;
            color: #34495e;
        }
    </style>
    <script src="https://unpkg.com/san@latest/dist/san.dev.js"></script>
</head>

<body>
    <h1>事件处理</h1>
    <div id="app"></div>
    <script>
        var MySon = san.defineComponent({
            template: `
            <div>
                <label for="msg">子组件
                    <input type="text" id="msg" value="{= msg =}">
                </label>
                <button on-click="submitMsg" id="btn" type="button">通知父组件</button>
            </div>
            `,
            initData: function () {
                return {
                    msg: ''
                }
            },
            submitMsg: function () {
                var msg = this.data.get("msg");
                this.dispatch("UI:msgChange", msg);
            },
            created() {
                this.data.set('msg', '');
            }
        });
        var MyFather = san.defineComponent({
            components: {
                'my-son': MySon
            },
            template: `
            <div>
                <my-son msg="{{ famsg }}"></my-son>
                <p style="color: yellowgreen">我是父组件：<input style="color: yellowgreen" type="text" value="{= famsg =}"></p>
            </div>
            `,
            initData: function () {
                return {
                    famsg: "静静等待用户输入"
                }
            },
            messages: {
                "UI:msgChange": function (arg) {
                    var value = arg.value;
                    this.data.set("famsg", value);
                    this.dispatch("UI:msgChange", value);
                }
            }
        });
        var MyApp = san.defineComponent({
            components: {
                'my-father': MyFather
            },
            template: `
            <div>
                <my-father famsg="{{ gfmsg }}"></my-father>
                <p style="color: darkorchid">我是更高层父组件：<input style="color: yellowgreen" type="text" value="{= gfmsg =}"></p>
            </div>
            `,
            initData: function () {
                return {
                    gfmsg: "我也静静等待用户输入"
                }
            },
            messages: {
                "UI:msgChange": function (arg) {
                    var value = arg.value;
                    this.data.set("gfmsg", value);
                }
            }
        });
        var myApp = new MyApp();
        myApp.attach(document.getElementById("app"));
    </script>
</body>

</html>